package flst.ipiweb.struts.action.paiement;


import ipiwebprocess.TypePaiementProcess;
import ipiwebtransverse.TypePaiement;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;



public class TypePaiementAction extends Action{

	
	private static TypePaiementProcess sTypePaiementProcess = new TypePaiementProcess();

	public ActionForward execute(ActionMapping mapping, 
			ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response)
			throws Exception {

		TypePaiementFormAction vTypePaiementForm = (TypePaiementFormAction) form;
		
		int tab[] = {
						new Integer(vTypePaiementForm.getIdCarteBancaire()).intValue(), 
						new Integer(vTypePaiementForm.getIdPayPal()).intValue(),
						new Integer(vTypePaiementForm.getIdCheque()).intValue(),
						new Integer(vTypePaiementForm.getIdContreRemboursement()).intValue()
					};
		

		TypePaiement vTypePaiementCB = sTypePaiementProcess.getTypePaiementById(tab[0]);
		TypePaiement vTypePaiementPayPal = sTypePaiementProcess.getTypePaiementById(tab[1]);
		TypePaiement vTypePaiementCheque = sTypePaiementProcess.getTypePaiementById(tab[2]);
		TypePaiement vTypePaiementCR = sTypePaiementProcess.getTypePaiementById(tab[3]);
		
		for (int i = 0; i < tab.length ; i++) {
			switch (i) {
			case 0:
				vTypePaiementCB.setAccessibleClient(vTypePaiementForm.isAccessibleClientCarteBancaire());
				vTypePaiementCB.setAccessibleSuperClient(vTypePaiementForm.isAccessibleSuperClientCarteBancaire());

				break;

			case 1:
				
				vTypePaiementPayPal.setAccessibleClient(vTypePaiementForm.isAccessibleClientPayPal());
				vTypePaiementPayPal.setAccessibleSuperClient(vTypePaiementForm.isAccessibleSuperClientPayPal());

				break;

			case 2:
				
				vTypePaiementCheque.setAccessibleClient(vTypePaiementForm.isAccessibleClientCheque());
				vTypePaiementCheque.setAccessibleSuperClient(vTypePaiementForm.isAccessibleSuperClientCheque());
				
				break;
				
			case 3:				
				vTypePaiementCR.setAccessibleClient(vTypePaiementForm.isAccessibleClientContreRemboursement());
				vTypePaiementCR.setAccessibleSuperClient(vTypePaiementForm.isAccessibleSuperClientContreRemboursement());

				break;
			}
			
		}
		
		sTypePaiementProcess.modifierTypePaiement(vTypePaiementCB);
		sTypePaiementProcess.modifierTypePaiement(vTypePaiementCR);
		sTypePaiementProcess.modifierTypePaiement(vTypePaiementCheque);
		sTypePaiementProcess.modifierTypePaiement(vTypePaiementPayPal);
		
		List<TypePaiement> listeTypePaiement = sTypePaiementProcess.listerTypePaiement();
		request.setAttribute("listeTypePaiement", listeTypePaiement);

		return mapping.findForward("typePaiement_ok");
	}
}
